/*
 * @Author: 生俊甫 1758142861@qq.com
 * @Date: 2024-10-11 20:44:29
 * @LastEditors: 生俊甫 1758142861@qq.com
 * @LastEditTime: 2024-10-11 21:16:35
 * @FilePath: /sjf/2024-project/2024_centos/test_to_c/tree_test/queue.c
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include "queue.h"


void QueueInit(queue* pq)
{
    pq->_head = pq->_tail = NULL;
}

void QueueDestroy(queue* pq)
{
    qnode* cur = pq->_head;
    while(cur)
    {
        qnode* next = cur->_next;
        free(cur);
        cur = next;
    }
}

void QueuePush(queue* pq, QDataType x)
{
    qnode* newnode = (qnode*)malloc(sizeof(qnode));
    newnode->_data = x;
    newnode->_next = NULL;
    if(pq->_head == NULL)
        pq->_head = pq->_tail = newnode;
    else
    {
        pq->_tail->_next = newnode;
        pq->_tail = newnode;
    }
}

void QueuePop(queue* pq)
{
    if(pq->_head->_next == NULL)
    {
        free(pq->_head);
        pq->_head = pq->_tail = NULL;
    }    
    else
    {
        qnode* cur = pq->_head->_next;
        pq->_head = cur;
    }
    
}
//取队头的数据
QDataType QueueFront(queue* pq)
{
    return pq->_head->_data;
}
//取队尾的数据
QDataType QueueBack(queue* pq)
{
    return pq->_tail->_data;
}
//判断队列是否为空
bool QueueEmpty(queue* pq)
{
    return pq->_head == NULL;
}

int QueueSize(queue* pq)
{
    int count = 0;
    qnode* cur = pq->_head;
    while(cur)
    {
        count++;
        cur = cur->_next;
    }
    return count;
}